package com.SouthernWall_404.AgeRight.Common.Network.Packets.S2C;

import com.SouthernWall_404.AgeRight.Common.API.Capability.Locks;
import com.SouthernWall_404.AgeRight.Common.API.Capability.Provider.LocksProvider;
import com.SouthernWall_404.AgeRight.Common.Network.Packets.S2C.ClientHandler.LocksClientHandler;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.network.NetworkEvent;

import java.util.UUID;
import java.util.function.Supplier;

public class LockS2CPack {

    public CompoundTag modPack=new CompoundTag();
    public ChunkPos chunkPos=new ChunkPos(0,0);

    public LockS2CPack(LevelChunk levelChunk)
    {
        Locks locks=levelChunk.getCapability(LocksProvider.LOCKS).orElse(null);
        if(locks!=null)
        {
            modPack=locks.serializeNBT();
            chunkPos=levelChunk.getPos();
        }
    }

    public LockS2CPack(FriendlyByteBuf buf) {
        this.modPack=buf.readNbt();
        this.chunkPos=buf.readChunkPos();
    }

    public void encode(FriendlyByteBuf buf) {
        buf.writeNbt(this.modPack);
        buf.writeChunkPos(this.chunkPos);
    }
    public void handle(Supplier<NetworkEvent.Context> ctx)
    {
        ctx.get().enqueueWork(() ->
                // 确保其仅在物理客户端上执行
                DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> LocksClientHandler.hanlder(this,ctx))
        );
        ctx.get().setPacketHandled(true);
    }
}

